package com.xhkj.odps;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.aliyun.odps.Odps;
import com.aliyun.odps.PartitionSpec;
import com.aliyun.odps.account.Account;
import com.aliyun.odps.account.AliyunAccount;
import com.aliyun.odps.data.Record;
import com.aliyun.odps.data.RecordWriter;
import com.aliyun.odps.tunnel.TableTunnel;
import com.aliyun.odps.tunnel.TableTunnel.UploadSession;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hikvision.artemis.sdk.kafka.entity.parse.result.VehicleAlarmData;
import com.hikvision.artemis.sdk.kafka.entity.parse.result.VehicleListData;
import com.hikvision.artemis.sdk.kafka.entity.parse.result.VehiclePassData;
import com.xhkj.model.MyVehicleNoPassData;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class UploadVehicleThread extends Thread {
	private static String accessId = "WB5ePNSftUOLzPoZ";
	private static String accessKey = "hUezl1X21Y7o6b6t9NK4VGz5FFzXAc";
	private static String odpsUrl = "http://service.cn-hs-hsgay-d01.odps.ops.hsgay/api";
	private static String tunnelUrl = "http://dt.cn-hs-hsgay-d01.odps.ops.hsgay";
	private static String partition = "dt";
	// 默认情况下，使用公网进行传输。如果需要通过内网进行数据传输，必须设置tunnelUrl变量。
	private static String project = "hengshuigongan";
	private static String table = "std_hkws_vehicle";

	private static ObjectMapper mapper = new ObjectMapper();
	
	private List<MyVehicleNoPassData> list = new ArrayList<MyVehicleNoPassData>();
	public UploadVehicleThread(List<MyVehicleNoPassData> listByState) {
		this.list = listByState;
	}
	@Override
	public void run() {
		Account account = new AliyunAccount(accessId, accessKey);
		Odps odps = new Odps(account);
		odps.setEndpoint(odpsUrl);
		odps.setDefaultProject(project);
		Date date = new Date();
		DateFormat format = new SimpleDateFormat("yyyyMMdd");
		String now = format.format(date);
		try {
			TableTunnel tunnel = new TableTunnel(odps);
			tunnel.setEndpoint(tunnelUrl);
			PartitionSpec partitionSpec = new PartitionSpec();
			partitionSpec.set(partition, now);
			UploadSession session = tunnel.createUploadSession(project, table, partitionSpec);
			RecordWriter recordWriter = session.openBufferedWriter();
			for(MyVehicleNoPassData data:list) {
				Record record = session.newRecord();
				try {
					if (data != null) {
						getPassRecord(record, "vehiclepassnorecoglist_", data);
					}
//					if (data != null && data.getVehiclePassRecogList() != null && data.getVehiclePassRecogList().size() > 0) {
//						getPassRecord(record, "vehiclepassrecoglist_", data.getVehiclePassRecogList().get(0));
//					}
//					if (data != null && data.getVehiclePassNoRecogList() != null && data.getVehiclePassNoRecogList().size() > 0) {
//						getPassRecord(record, "vehiclepassnorecoglist_", data.getVehiclePassNoRecogList().get(0));
//					}
//					if (data != null && data.getVehicleAlarmRecogList() != null && data.getVehicleAlarmRecogList().size() > 0) {
//						getAlarmRecord(record, "vehiclealarmrecoglist_", data.getVehicleAlarmRecogList().get(0));
//					}
//					if (data != null && data.getVehicleAlarmNoRecogList() != null && data.getVehicleAlarmNoRecogList().size() > 0) {
//						getAlarmRecord(record, "vehiclealarmnorecoglist_", data.getVehicleAlarmNoRecogList().get(0));
//					}
					recordWriter.write(record);
				} catch (Exception e) {
					e.printStackTrace();
					log.error("data error ：" + mapper.writeValueAsString(data));
					continue;
					// TODO Auto-generated catch block
				}
			}
			recordWriter.close();
			session.commit();
			log.info("ali upload vehicle data success" + this.getName());
		} catch (Exception e) {
			// 建议重试一定次数。
			log.error("ali upload vehicle data error", e);	
			e.printStackTrace();
		}
	}

	private static void getPassRecord(Record record, String qz, MyVehicleNoPassData data) {
		record.set(qz + "rect", data.getRect());
		record.set(qz + "plateno", data.getPlateNo());
		record.set(qz + "crossingindexcode", data.getCrossingIndexCode());
		record.set(qz + "cameraindexcode", data.getCameraIndexCode());
		record.set(qz + "laneno", data.getLaneNo());
		record.set(qz + "id", data.getId());
		record.set(qz + "platecolor", data.getPlateColor());
		record.set(qz + "platecolorname", data.getPlateColorName());
		record.set(qz + "platetype", data.getPlateType());
		record.set(qz + "platetypename", data.getPlateTypeName());
		record.set(qz + "platestate", data.getPlateState());
		record.set(qz + "platestatename", data.getPlateStateName());
		record.set(qz + "plateimagepath", data.getPlateImagePath());
		record.set(qz + "tempplateno", data.getTempPlateNo());
		record.set(qz + "tempplatenoname", data.getTempPlateNoName());
		record.set(qz + "vehiclecolor", data.getVehicleColor());
		record.set(qz + "vehiclecolorname", data.getVehicleColorName());
		record.set(qz + "vehicletype", data.getVehicleType());
		record.set(qz + "vehicletypename", data.getVehicleTypeName());
		record.set(qz + "vehiclecolordepth", data.getVehicleColorDepth());
		record.set(qz + "vehiclecolordepthname", data.getVehicleColorDepthName());
		record.set(qz + "vehiclelogo", data.getVehicleLogo());
		record.set(qz + "vehiclesublogo", data.getVehicleSubLogo());
		record.set(qz + "vehiclemodel", data.getVehicleModel());
		record.set(qz + "vehiclelamp", data.getVehicleLamp());
		record.set(qz + "vehiclelampname", data.getVehicleLampName());
		record.set(qz + "vehiclespeed", data.getVehicleSpeed());
		record.set(qz + "copilot", data.getCopilot());
		record.set(qz + "copilotname", data.getCopilotName());
		record.set(qz + "pilotsafebelt", data.getPilotSafebelt());
		record.set(qz + "pliotsafebeltname", data.getPliotSafebeltName());
		record.set(qz + "vicepilotsafebelt", data.getVicePilotSafebelt());
		record.set(qz + "vicepilotsafebeltname", data.getVicePilotSafebeltName());
		record.set(qz + "pilotsunvisor", data.getPilotSunvisor());
		record.set(qz + "pilotsunvisorname", data.getPilotSunvisorName());
		record.set(qz + "vicepilotsunvisor", data.getVicePilotSunvisor());
		record.set(qz + "vicepilotsunvisorname", data.getVicePilotSunvisorName());
		record.set(qz + "frontchild", data.getFrontChild());
		record.set(qz + "frontchildname", data.getFrontChildName());
		record.set(qz + "sunroof", data.getSunroof());
		record.set(qz + "sunroofname", data.getSunroofName());
		record.set(qz + "pdvs", data.getPdvs());
		record.set(qz + "pdvsname", data.getPdvsName());
		record.set(qz + "uphone", data.getUphone());
		record.set(qz + "usephonename", data.getUsePhoneName());
		record.set(qz + "mobiledevicelongitude", data.getMobileDeviceLongitude());
		record.set(qz + "mobiledevicelatitude", data.getMobileDeviceLatitude());
		record.set(qz + "capturetime", data.getCaptureTime());
		record.set(qz + "vehiclehead", data.getVehicleHead());
		record.set(qz + "vehicleheadname", data.getVehicleHeadName());
		record.set(qz + "luggagerack", data.getLuggageRack());
		record.set(qz + "luggagerackname", data.getLuggageRackName());
		record.set(qz + "vehiclespraypainted", data.getVehicleSprayPainted());
		record.set(qz + "vehiclespraypaintedname", data.getVehicleSprayPaintedName());
		record.set(qz + "sparetire", data.getSpareTire());
		record.set(qz + "sparetirename", data.getSpareTireName());
		record.set(qz + "card", data.getCard());
		record.set(qz + "cardname", data.getCardName());
		record.set(qz + "cardtype", data.getCardType());
		record.set(qz + "cardtypename", data.getCardTypeName());
		record.set(qz + "cardnum", data.getCardNum());
		record.set(qz + "mucktruck", data.getMuckTruck());
		record.set(qz + "mucktruckname", data.getMuckTruckName());
		record.set(qz + "coverplate", data.getCoverPlate());
		record.set(qz + "coverplatename", data.getCoverPlateName());
		record.set(qz + "tricyclecanopy", data.getTricycleCanopy());
		record.set(qz + "tricyclecanopyname", data.getTricycleCanopyName());
		record.set(qz + "ecolabel", data.getEcolabel());
		record.set(qz + "ecolabelname", data.getEcolabelName());
		record.set(qz + "pendant", data.getPendant());
		record.set(qz + "pendantname", data.getPendantName());
		record.set(qz + "label", data.getLabel());
		record.set(qz + "labelname", data.getLabelName());
		record.set(qz + "tissuebox", data.getTissueBox());
		record.set(qz + "tissueboxname", data.getTissueBoxName());
		record.set(qz + "decoration", data.getDecoration());
		record.set(qz + "decorationname", data.getDecorationName());
		record.set(qz + "dangmark", data.getDangMark());
		record.set(qz + "dangmarkname", data.getDangMarkName());
		record.set(qz + "envprosign", data.getEnvproSign());
		record.set(qz + "envprosignname", data.getEnvproSignName());
		record.set(qz + "directionindex", data.getDirectionIndex());
		record.set(qz + "directionname", data.getDirectionName());
		record.set(qz + "labelnum", data.getLabelNum());
		record.set(qz + "linkfacevehicleid", data.getLinkFaceVehicleId());
		record.set(qz + "linkwifivehicleid", data.getLinkWifiVehicleId());
		record.set(qz + "picurlnum", data.getPicUrlNum() + "");
		record.set(qz + "imagepath1", data.getImagePath1());
		record.set(qz + "imagepath2", data.getImagePath2());
		record.set(qz + "imagepath3", data.getImagePath3());
		record.set(qz + "imagepath4", data.getImagePath4());
		record.set(qz + "imagepath5", data.getImagePath5());
		record.set(qz + "imagepath6", data.getImagePath6());
		record.set(qz + "targetpicurl", data.getTargetPicUrl());
		record.set(qz + "direction", data.getDirection());
	}

	private static void getAlarmRecord(Record record, String qz, VehicleAlarmData data) {
		record.set(qz + "rect", data.getRect());
		record.set(qz + "plateno", data.getPlateNo());
		record.set(qz + "crossingindexcode", data.getCrossingIndexCode());
		record.set(qz + "cameraindexcode", data.getCameraIndexCode());
		record.set(qz + "laneno", data.getLaneNo());
		record.set(qz + "id", data.getId());
		record.set(qz + "platecolor", data.getPlateColor());
		record.set(qz + "platecolorname", data.getPlateColorName());
		record.set(qz + "platetype", data.getPlateType());
		record.set(qz + "platetypename", data.getPlateTypeName());
		record.set(qz + "platestate", data.getPlateState());
		record.set(qz + "platestatename", data.getPlateStateName());
		record.set(qz + "plateimagepath", data.getPlateImagePath());
		record.set(qz + "tempplateno", data.getTempPlateNo());
		record.set(qz + "tempplatenoname", data.getTempPlateNoName());
		record.set(qz + "vehiclecolor", data.getVehicleColor());
		record.set(qz + "vehiclecolorname", data.getVehicleColorName());
		record.set(qz + "vehicletype", data.getVehicleType());
		record.set(qz + "vehicletypename", data.getVehicleTypeName());
		record.set(qz + "vehiclecolordepth", data.getVehicleColorDepth());
		record.set(qz + "vehiclecolordepthname", data.getVehicleColorDepthName());
		record.set(qz + "vehiclelogo", data.getVehicleLogo());
		record.set(qz + "vehiclesublogo", data.getVehicleSubLogo());
		record.set(qz + "vehiclemodel", data.getVehicleModel());
		record.set(qz + "vehiclelamp", data.getVehicleLamp());
		record.set(qz + "vehiclelampname", data.getVehicleLampName());
		record.set(qz + "vehiclespeed", data.getVehicleSpeed());
		record.set(qz + "copilot", data.getCopilot());
		record.set(qz + "copilotname", data.getCopilotName());
		record.set(qz + "pilotsafebelt", data.getPilotSafebelt());
		record.set(qz + "pliotsafebeltname", data.getPliotSafebeltName());
		record.set(qz + "vicepilotsafebelt", data.getVicePilotSafebelt());
		record.set(qz + "vicepilotsafebeltname", data.getVicePilotSafebeltName());
		record.set(qz + "pilotsunvisor", data.getPilotSunvisor());
		record.set(qz + "pilotsunvisorname", data.getPilotSunvisorName());
		record.set(qz + "vicepilotsunvisor", data.getVicePilotSunvisor());
		record.set(qz + "vicepilotsunvisorname", data.getVicePilotSunvisorName());
		record.set(qz + "frontchild", data.getFrontChild());
		record.set(qz + "frontchildname", data.getFrontChildName());
		record.set(qz + "sunroof", data.getSunroof());
		record.set(qz + "sunroofname", data.getSunroofName());
		record.set(qz + "pdvs", data.getPdvs());
		record.set(qz + "pdvsname", data.getPdvsName());
		record.set(qz + "uphone", data.getUphone());
		record.set(qz + "usephonename", data.getUsePhoneName());
		record.set(qz + "mobiledevicelongitude", data.getMobileDeviceLongitude());
		record.set(qz + "mobiledevicelatitude", data.getMobileDeviceLatitude());
		record.set(qz + "capturetime", data.getCaptureTime());
		record.set(qz + "vehiclehead", data.getVehicleHead());
		record.set(qz + "vehicleheadname", data.getVehicleHeadName());
		record.set(qz + "luggagerack", data.getLuggageRack());
		record.set(qz + "luggagerackname", data.getLuggageRackName());
		record.set(qz + "vehiclespraypainted", data.getVehicleSprayPainted());
		record.set(qz + "vehiclespraypaintedname", data.getVehicleSprayPaintedName());
		record.set(qz + "sparetire", data.getSpareTire());
		record.set(qz + "sparetirename", data.getSpareTireName());
		record.set(qz + "card", data.getCard());
		record.set(qz + "cardname", data.getCardName());
		record.set(qz + "cardtype", data.getCardType());
		record.set(qz + "cardtypename", data.getCardTypeName());
		record.set(qz + "cardnum", data.getCardNum());
		record.set(qz + "mucktruck", data.getMuckTruck());
		record.set(qz + "mucktruckname", data.getMuckTruckName());
		record.set(qz + "coverplate", data.getCoverPlate());
		record.set(qz + "coverplatename", data.getCoverPlateName());
		record.set(qz + "tricyclecanopy", data.getTricycleCanopy());
		record.set(qz + "tricyclecanopyname", data.getTricycleCanopyName());
		record.set(qz + "ecolabel", data.getEcolabel());
		record.set(qz + "ecolabelname", data.getEcolabelName());
		record.set(qz + "pendant", data.getPendant());
		record.set(qz + "pendantname", data.getPendantName());
		record.set(qz + "label", data.getLabel());
		record.set(qz + "labelname", data.getLabelName());
		record.set(qz + "tissuebox", data.getTissueBox());
		record.set(qz + "tissueboxname", data.getTissueBoxName());
		record.set(qz + "decoration", data.getDecoration());
		record.set(qz + "decorationname", data.getDecorationName());
		record.set(qz + "dangmark", data.getDangMark());
		record.set(qz + "dangmarkname", data.getDangMarkName());
		record.set(qz + "envprosign", data.getEnvproSign());
		record.set(qz + "envprosignname", data.getEnvproSignName());
		record.set(qz + "directionindex", data.getDirectionIndex());
		record.set(qz + "directionname", data.getDirectionName());
		record.set(qz + "labelnum", data.getLabelNum());
		record.set(qz + "linkfacevehicleid", data.getLinkFaceVehicleId());
		record.set(qz + "linkwifivehicleid", data.getLinkWifiVehicleId());
		record.set(qz + "picurlnum", data.getPicUrlNum());
		record.set(qz + "imagepath1", data.getImagePath1());
		record.set(qz + "imagepath2", data.getImagePath2());
		record.set(qz + "imagepath3", data.getImagePath3());
		record.set(qz + "imagepath4", data.getImagePath4());
		record.set(qz + "imagepath5", data.getImagePath5());
		record.set(qz + "imagepath6", data.getImagePath6());
		record.set(qz + "targetpicurl", data.getTargetPicUrl());
		record.set(qz + "direction", data.getDirection());

		record.set(qz + "alarmId", data.getAlarmId());
		record.set(qz + "alarmType", data.getAlarmType());

	}
}